unit Uconsulta;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, Buttons, StdCtrls, Grids, DB, ZAbstractRODataset,
  ZAbstractDataset, ZDataset;

type
  TfrmConsulta = class(TForm)
    Panel1: TPanel;
    ed_apelido: TEdit;
    ck_apelido: TCheckBox;
    ck_nome: TCheckBox;
    ed_nome: TEdit;
    ck_telefone: TCheckBox;
    ed_telefone: TEdit;
    bt_procurar: TSpeedButton;
    ck_numero: TCheckBox;
    ed_numero: TEdit;
    resultados: TStringGrid;
    procedure bt_procurarClick(Sender: TObject);
    procedure ed_numeroEnter(Sender: TObject);
    procedure ed_apelidoEnter(Sender: TObject);
    procedure ed_nomeEnter(Sender: TObject);
    procedure ed_telefoneEnter(Sender: TObject);
    procedure FormKeyUp(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure FormShow(Sender: TObject);
    procedure resultadosDblClick(Sender: TObject);
    procedure resultadosClick(Sender: TObject);
    procedure resultadosDrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmConsulta: TfrmConsulta;

implementation

uses Udm,UCliente;

{$R *.dfm}

procedure AutoSizeGrid(Grid: TStringGrid);
const
  ColWidthMin = 10;
var
  C, R, W, ColWidthMax: integer;
begin
  for C := 0 to Grid.ColCount - 1 do begin
    ColWidthMax := ColWidthMin;
    for R := 0 to (Grid.RowCount - 1) do begin
      W := Grid.Canvas.TextWidth(Grid.Cells[C, R]);
      if W > ColWidthMax then
        ColWidthMax := W;
    end;
    Grid.ColWidths[C] := ColWidthMax + 5;
  end;
end;

procedure TfrmConsulta.bt_procurarClick(Sender: TObject);
var sql:tzquery;
    i,i2:word;
begin
 sql := tzquery.create(Application);
 sql.Connection := dm.conexao;
 sql.Sql.Clear;
 sql.Sql.Add('Select codigo,apelido,nome,telefone from cadastro WHERE');
  if ck_numero.Checked then begin
   sql.Sql.Add('codigo='+ed_numero.Text); sql.sql.add('AND'); end;
  if ck_apelido.Checked then begin
   sql.Sql.Add('apelido like "%'+ed_apelido.Text+'%"'); sql.sql.add('AND'); end;
  if ck_nome.Checked then begin
   sql.Sql.Add('nome like "%'+ed_nome.Text+'%"'); sql.sql.add('AND'); end;
  if ck_telefone.Checked then begin
   sql.Sql.Add('telefone like "%'+ed_telefone.Text+'%"'); sql.sql.add('AND'); end;
  if sql.SQL[sql.SQL.Count-1] = 'AND' then sql.SQL.Delete(sql.SQL.Count-1);
 sql.Open;
 resultados.RowCount := sql.RecordCount+1;
 resultados.ColCount := sql.fields.Count;
 for i := 0 to sql.fields.Count -1 do resultados.Cells[i,0] := sql.fields[i].FieldName;
  if sql.RecordCount > 0 then begin
   for i := 1 to sql.RecordCount do begin
    for i2 := 0 to sql.Fields.Count -1 do resultados.Cells[i2,i] := sql.fields[i2].asString;
    sql.Next;
   end;
  end;
 sql.Close;
 sql.Free;
 autosizegrid(resultados);
end;

procedure TfrmConsulta.ed_numeroEnter(Sender: TObject);
begin
ck_numero.Checked := true;
end;

procedure TfrmConsulta.ed_apelidoEnter(Sender: TObject);
begin
ck_apelido.Checked :=true;
end;

procedure TfrmConsulta.ed_nomeEnter(Sender: TObject);
begin
ck_nome.Checked := true;
end;

procedure TfrmConsulta.ed_telefoneEnter(Sender: TObject);
begin
ck_telefone.Checked :=true;
end;

procedure TfrmConsulta.FormKeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
if key = VK_RETURN then bt_procurarClick(nil);
end;

procedure TfrmConsulta.FormShow(Sender: TObject);
begin
 ed_numero.SelectAll;
 ed_numero.SetFocus;
end;

procedure TfrmConsulta.resultadosDblClick(Sender: TObject);
begin
 try
  if resultados.cells[0,resultados.Row] <> '' then begin
   frmCliente.abre_cadastro(strtoint(resultados.cells[0,resultados.Row]));
   close;
  end;
 except
 end;
end;

procedure TfrmConsulta.resultadosClick(Sender: TObject);
begin
 try
  resultados.selection := TGridRect(Rect(0,resultados.Row,resultados.colcount,resultados.row));
 except
 end;
end;

procedure TfrmConsulta.resultadosDrawCell(Sender: TObject; ACol,
  ARow: Integer; Rect: TRect; State: TGridDrawState);
var
  TempPString:Array [0..255] of char;
begin
If (gdFixed in State){or (gdSelected in State)} then exit;
resultados.Canvas.Brush.Style:=bsSolid;
if not odd(aRow) then resultados.Canvas.Brush.Color:=clSilver;
resultados.Canvas.FillRect(Rect);
StrPCopy(TempPString,resultados.Cells[ACol,ARow]);
DrawText(resultados.Canvas.Handle,TempPString,-1,Rect,DT_LEFT);
end;

end.
